package com.example.xyr.myapplication.ui.im

import android.app.Activity
import android.content.Intent
import android.view.View
import cn.jpush.im.android.api.callback.DownloadCompletionCallback
import cn.jpush.im.android.api.content.ImageContent
import cn.jpush.im.android.api.model.Message
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.utils.LogUtil
import com.example.xyr.myapplication.utils.MyIntent
import com.google.gson.jpush.Gson
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_image.*
import kotlinx.android.synthetic.main.titlebar_layout.*
import java.io.File

/**
 * Created by ylcf on 2018/8/22.
 */
class ImageActivity : BaseActivity(), View.OnClickListener {


    companion object {
        lateinit var message1: Message
        fun initActivity(fromActivity: Activity, message: Message) {
            var intent = Intent(fromActivity, ImageActivity::class.java)
            message1 = message
            fromActivity.startActivity(intent)
        }
    }

    override fun getLayout(): Int = R.layout.activity_image


    override fun init() {

    }

    override fun initListener() {
        titleBar.ivBack.setOnClickListener(this)
    }

    override fun initDate(intent: MyIntent?) {
        if (message1 != null) {
            val content = Gson().fromJson<ImageContent>(message1.content.toJson(), ImageContent::class.java)
            content.downloadOriginImage(message1, object : DownloadCompletionCallback() {
                override fun onComplete(p0: Int, p1: String?, p2: File?) = if (p0 == 0) {
                    var pathStr:String = p2!!.path
                    LogUtil.e("tag", "图片位置==》》》》》${pathStr}")
                    Picasso.with(applicationContext)
                            .load(p2)
                            .error(R.drawable.ic_launcher)
                            .into(ziv_image)
                }else{
                    toastNo("图片加载失败=${p1}")
                }
            })
        }
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.iv_back -> finish()
        }

    }
}